package com.pat.web.controller;

import java.util.Date;
import java.util.Random;

import org.apache.commons.codec.digest.DigestUtils;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.render.JsonRender;
import com.pat.common.util.CryptUtil;
import com.pat.common.util.SmsUtil;
import com.pat.web.ApiResponse;
import com.pat.web.model.LoginStatus;
import com.pat.web.model.User;

public class LoginController  extends Controller{

	public void sendSms(){
		String phone = getPara("mobile");
		Random r = new Random();
		String code = String.valueOf(r.nextInt(9000)+1000);
		sendSms(phone,code);
		//5分钟生效
		setSessionAttr(phone+"_sms"+(System.currentTimeMillis()/1000/60/5), code);
		
		
		ApiResponse response = new ApiResponse();
		response.setStatusCode("0000");
		response.setData("success");
		render(new JsonRender(response));
	}
	
	public void loginByPWD(){
		User user = getBean(User.class,"",true);
		String password = user.getPassword();
		String digestPWD = DigestUtils.sha256Hex(password.getBytes());
		
		User result = User.dao.findFirst("select * from user where mobile=? and password = ?" , user.getMobile(),digestPWD);
		ApiResponse response = new ApiResponse();
		if(result==null){
			response.setStatusCode("1002");
			response.setData("error");
		}else{
			String userToken = CryptUtil.encrypt(String.valueOf(result.getId()+"_"+System.currentTimeMillis()));
			
			writeLogonStatus(result, userToken);
			
			response.setStatusCode("0000");
			
			JSONObject json = new JSONObject();
			json.put("userToken", userToken);
			response.setData(json);
		}
		render(new JsonRender(response));
	}

	public void loginBySms(){
		String mobile = getPara("mobile");
		String code  = getPara("code");
		
		String storedKey = mobile+"_sms"+(System.currentTimeMillis()/1000/60/5);
		String sessionCode = getSessionAttr(storedKey);
		if(code==null || !code.equals(sessionCode)){
			ApiResponse response = new ApiResponse();
			response.setStatusCode("1003");
			response.setData("login fail");
			render(new JsonRender(response));
			return ;
		}
		
		User user = User.dao.findFirst("select * from user where mobile = ?", mobile);
		if(user == null){
			 user = new User();
			 user.setMobile(mobile);
			 user.save();
		}
		
		String userToken = CryptUtil.encrypt(String.valueOf(user.getId()+"_"+System.currentTimeMillis()));
		
		writeLogonStatus(user, userToken);
		
		ApiResponse response = new ApiResponse();
		response.setStatusCode("0000");
		
		JSONObject json = new JSONObject();
		json.put("userToken", userToken);
		response.setData(json);
		render(new JsonRender(response));
	}
	
	
	public void register(){
		User user = getBean(User.class,"", true);
		
		String storedKey = user.getMobile()+"_sms"+(System.currentTimeMillis()/1000/60/5);
		String code = getPara("code");
		String sessionCode = getSessionAttr(storedKey);
		if(code==null || !code.equals(sessionCode)){
			ApiResponse response = new ApiResponse();
			response.setStatusCode("1003");
			response.setData("register fail");
			render(new JsonRender(response));
			return ;
		}
		
		User existed = User.dao.findFirst("select * from user where mobile = ?", user.getMobile());
		
		if(existed!=null){
			ApiResponse response = new ApiResponse();
			response.setStatusCode("1004");
			render(new JsonRender(response));
			return ;
		}
		
		String password = user.getPassword();
		String digestPWD = DigestUtils.sha256Hex(password.getBytes());
		user.setPassword(digestPWD);
		user.setCreateTime(new Date());
		user.setUpdateTime(new Date());
		user.setSex(0);
		user.save();
		String userToken = CryptUtil.encrypt(String.valueOf(user.getId()+"_"+System.currentTimeMillis()));
		
		LoginStatus loginStatus = new LoginStatus();
		loginStatus.setUserId(user.getId()).setCreateTime(new Date()).setUpdateTime(new Date())
		.setUserToken(userToken).save();
		
		ApiResponse response = new ApiResponse();
		response.setStatusCode("0000");
		
		JSONObject json = new JSONObject();
		json.put("userToken", userToken);
		response.setData(json);
		render(new JsonRender(response));
	}
	
	private void sendSms(String phone,String code){
		SmsUtil.sendVerifyCode(phone, code, "5");
	}
	
	private void writeLogonStatus(User result, String userToken) {
		LoginStatus loginStatus = LoginStatus.dao.findFirst("select * from login_status where user_id = ?",result.getId());
		
		if(loginStatus==null){
			loginStatus = new LoginStatus();
			loginStatus.setCreateTime(new Date()).setUpdateTime(new Date()).setUserId(result.getId());
			loginStatus.setUserToken(userToken).save();
		}else{
			loginStatus.setUpdateTime(new Date());
			loginStatus.setUserToken(userToken).update();
		}
	}
	
}
